

import UIKit
@objcMembers class FAMEncryptTool: NSObject {
    static func encrypt(content: Any) -> String {
        var contentStr = ""
        if let str = content as? String {
            contentStr = str
        } else {
            contentStr = FAMEncryptTool.convertToJSONString(object: content)
        }
        
        let encryp = FAMEncryptor.fam_AesEncrypt(contentStr, withKey: self.aesKey() , iv: self.aesKey())
        return encryp
    }
    static func decrypt(content: Any) -> String {
        var contentStr = ""
        if let str = content as? String {
            contentStr = str
        } else {
            contentStr = FAMEncryptTool.convertToJSONString(object: content)
        }
        let decryptString = FAMEncryptor.fam_AesDecrypt(contentStr, withKey: self.aesKey(), iv: self.aesKey())
        return decryptString
    }
    static func aesKey() -> String {
        return generateKey(appId: FAMTool.fam_getAppId(), appKey: FAMTool.fam_getAppKey()).lowercased()
    }
    
    static func generateKey(appId: String, appKey: String) -> String {
        guard !appId.isEmpty || !appKey.isEmpty else { return "" }
        return String(FAMMD5().MD5(appId + appKey).prefix(16))
    }
    
    static func convertToJSONString(object: Any) -> String {
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: object, options: [])
            guard let jsonString = String(data: jsonData, encoding: .utf8) else {
                return ""
            }
            return jsonString
        } catch {
            
            return ""
        }
    }
}

